EAS_JS.Tabs = {
	navbarBox : null,
	contentBox : null,
	currentTab : 0,
	lengthTab : 1,
	recycleTabs : [],
	tabCache : [],
	tabScriptCache : [],
	beforeCall : null,
	afterCall : null,
	
	init : function (opts) {
		for (option in opts) {
			this[option] = opts[option];
		}
	},

	setTitle : function (sTabTitle) {
		$("#_Tab_"+this.currentTab).html(sTabTitle + this.getCloseIcon(this.currentTab));
		$("#_Tab_"+this.currentTab).title = sTabTitle;
	},

	create : function (sContent){
		if (arguments.length > 1){
			var tabID = arguments[1];
		}
		else{
			var tabID = this.lengthTab;
			this.lengthTab++;
		}

		var span = "<span id='_Tab_" + tabID +"' tabNum='" + tabID + "' focusOn='1' blank='1'>空白页" + this.getCloseIcon(tabID) + "</span>";
		this.navbarBox.append(span);
	
		this.switchTab(tabID, sContent);
	},

	open : function (sContent){
		if (this.currentTab != 0)
		{
			this.switchTab(this.currentTab, sContent);
		}
		else{
			if (this.recycleTabs.length > 0)
			{
				this.create(sContent, this.recycleTabs[this.recycleTabs.length - 1]);
				this.recycleTabs.pop();
			}
			else{
				this.create(sContent);
			}
		}
	},

	switchTab : function (tabID){
		if (tabID == this.currentTab){
			return false;
		}
		//alert(this.switchTab.caller);
		//alert(tabID);
		
		var tabJoin = "," + this.recycleTabs.join(",") + ",";

		//切换标签
		if (tabJoin.indexOf("," + this.currentTab + ",") == -1){
			this.initTab("#_Tab_"+this.currentTab);
			
			this.tabCache[this.currentTab] = this.contentBox.html();
		}
		
		this.currentTab = tabID;
		
		var tabObj = $("#_Tab_"+tabID);
		tabObj.addClass("tabOn").removeClass("tabHover");
		tabObj.die("mouseover").die("mouseout").die("click");
		tabObj.attr({
			focusOn : "1",
			blank : "0"
		});

		if (this.beforeCall)
		{
			var t = this.beforeCall;

			t();
		}

		if (arguments.length > 1)
		{
			var sContent = arguments[1];

			if (typeof(sContent) == "undefined"){
				//新建标签
				returnMessage = "<div style='height: 300px;'></div>";
				sContent = "";

				tabObj.attr("blank", "1");
				isExeJS = false;
			}
			else{
				var regexp1 = /<script(.|\n)*?>(.|\n|\r\n)*?<\/script>/ig;
				var returnMessage = sContent.replace(regexp1, "");
				sContent = sContent.match(regexp1);
				isExeJS = true;
			}

			this.tabCache[tabID] = returnMessage;
			this.tabScriptCache[tabID] = sContent;
		}
		else{
			//切换标签
			var returnMessage = this.tabCache[tabID];
			var sContent = this.tabScriptCache[tabID];
			isExeJS = false;
		}

		this.contentBox.html(returnMessage);
		this.executeScript(sContent, isExeJS);

		if (this.afterCall)
		{
			var t = this.afterCall;

			t();
		}
	},

	remove : function (tabID){
		var isSwitch = $("#_Tab_"+tabID).attr("focusOn");

		$("#_Tab_"+tabID).die("click");
		$("#_Tab_"+tabID).remove();
		this.tabCache[tabID] = null;
		this.tabScriptCache[tabID] = null;
		this.recycleTabs.push(tabID);

		if (isSwitch == "1")
		{
			var tabJoin = "," + this.recycleTabs.join(",") + ",";
		
			for (var i = tabID - 1;i >= 0;i-- )
			{
				if (tabJoin.indexOf("," + i + ",") == -1)
				{
					this.switchTab(i);
					break;
				}
			}
		}
	},

	initTab : function (tabID) {
		var objTab = $(tabID);
		
		objTab.live("mouseover", function () {
			$(this).addClass("tabHover");
		}).live("mouseout", function () {
			$(this).removeClass("tabHover");
		}).live("click", function () {
			EAS_JS.Tabs.switchTab($(this).attr("tabNum"));
		});

		objTab.removeClass("tabOn");

		objTab.attr("focusOn", "0");
	},

	//插入内部脚本
	executeScript : function  (message){
		if (message) {
			var regexp2 = /<script(.|\n)*?>((.|\n|\r\n)*)?<\/script>/im;

			for (var i = 0; i < message.length; i++) {
				/* Note: do not try to write more than one <script> in your view.*/
				/* break;  process only one script element */
				var realScript = message[i].match(regexp2);
				
				eval(realScript[2]);
			}
		}

		if (arguments.length > 1)
		{
			if (arguments[1])
			{
				//initTab();
			}
		}
	},

	getCloseIcon : function (tabID) {
		return "&nbsp;<a href='javascript:;' onclick='javascript: EAS_JS.Tabs.remove("+tabID+");' title='关闭页面'>X</a>";
	}
}